package test;

import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.junit.Test;

import database.connector.DatabaseFactory;
import database.live.MySqlDAO;

/* 
 * 
 * The full class for wiping and setting all the data
 * 
 */

public class PopulateDB {

	@Test 
	public void truncateData()
	{
		MySqlDAO d = (MySqlDAO) DatabaseFactory.getDAO();
		if (d.isOpen())
		{
			d.update("TRUNCATE TABLE trackvoodoo.user");
			d.update("TRUNCATE TABLE trackvoodoo.newsitem");
			d.update("TRUNCATE TABLE trackvoodoo.track");
			d.update("TRUNCATE TABLE trackvoodoo.trackpictures");
			d.update("TRUNCATE TABLE trackvoodoo.trackdays");
			d.update("TRUNCATE TABLE trackvoodoo.car");
			d.update("TRUNCATE TABLE trackvoodoo.carpictures");
			d.update("TRUNCATE TABLE trackvoodoo.carcomments");
			d.update("TRUNCATE TABLE trackvoodoo.carmodifications");
			d.update("TRUNCATE TABLE trackvoodoo.usertrackdays");
			d.update("TRUNCATE TABLE trackvoodoo.carcomments");
			d.update("TRUNCATE TABLE trackvoodoo.achivement");
		}
	}
	
	
	@Test
	public void populateDatabaseUser() 
	{
		MySqlDAO d = (MySqlDAO) DatabaseFactory.getDAO();
		if (d.isOpen())
		{
			PreparedStatement ps = d.getPreparedStatement(PopulateData.USER_INJECT[0][0]);
			
			int length = PopulateData.USER_INJECT.length;
			for (int count=1 ; count < length; count++)
			{
				try {
				String[] userData = PopulateData.USER_INJECT[count];
				ps.setString(1, userData[0]);
				ps.setString(2, userData[1]);
				ps.setString(3, userData[2]);
				ps.setString(4, userData[3]);
				ps.setBoolean(5, new Boolean (userData[4]));
				
				ps.executeUpdate();
				}catch (SQLException e)
				{
					System.out.println("Cannnot insert data from row count" + count);
				}
				
			}	
			d.closeConnection();
		}
	}
	
	
	@Test
	public void populateNews()
	{
		MySqlDAO d = (MySqlDAO) DatabaseFactory.getDAO();
		if (d.isOpen())
		{
			PreparedStatement ps = d.getPreparedStatement(PopulateData.NEWS_INJECT[0][0]);
			int length = PopulateData.NEWS_INJECT.length;
			for (int count=1 ; count < length; count++)
			{
				try {
				String[] userData = PopulateData.NEWS_INJECT[count];
				ps.setString(1, userData[0]);
				ps.setString(2, userData[1]);
				ps.setString(3, userData[2]);				
				ps.executeUpdate();
				}catch (SQLException e)
				{
					System.out.println("Cannnot insert data from row count" + count);
				}
			}
			d.closeConnection();
		}
	}
	
	@Test
	public void populateTrackInject()
	{
		MySqlDAO d = (MySqlDAO) DatabaseFactory.getDAO();
		if (d.isOpen())
		{
			PreparedStatement ps = d.getPreparedStatement(PopulateData.TRACK_INJECT[0][0]);
			int length = PopulateData.TRACK_INJECT.length;
			for (int count=1 ; count < length; count++)
			{
				try {
				String[] userData = PopulateData.TRACK_INJECT[count];
				ps.setString(1, userData[0]);
				ps.setString(2, userData[1]);				
				ps.executeUpdate();
				}catch (SQLException e)
				{
					System.out.println(e.getMessage());
					System.out.println("Cannnot insert data from row count" + count);
				}
			}
			d.closeConnection();
		}
	}
	
	@Test
	public void populateTrackPicsInject()
	{
		MySqlDAO d = (MySqlDAO) DatabaseFactory.getDAO();
		if (d.isOpen())
		{
			PreparedStatement ps = d.getPreparedStatement(PopulateData.TRACKPICTURES_INJECT[0][0]);
			int length = PopulateData.TRACKPICTURES_INJECT.length;
			for (int count=1 ; count < length; count++)
			{
				try {
				String[] userData = PopulateData.TRACKPICTURES_INJECT[count];
				ps.setString(1, userData[0]);
				ps.setString(2, userData[1]);
				ps.setString(3, userData[2]);
				ps.setString(4, userData[3]);
				ps.executeUpdate();
				}catch (SQLException e)
				{
					System.out.println(e.getMessage());
					System.out.println("Cannnot insert data from row count" + count);
				}
			}
			d.closeConnection();
		}
	}
	
	@Test
	public void populateTrackDaysInject()
	{
		MySqlDAO d = (MySqlDAO) DatabaseFactory.getDAO();
		if (d.isOpen())
		{
			PreparedStatement ps = d.getPreparedStatement(PopulateData.TRACKDAYS_INJECT[0][0]);
			int length = PopulateData.TRACKDAYS_INJECT.length;
			for (int count=1 ; count < length; count++)
			{
				try {
				String[] userData = PopulateData.TRACKDAYS_INJECT[count];
				ps.setInt(1, new Integer(userData[0]));
				ps.setString(2, userData[1]);

				ps.executeUpdate();
				}catch (SQLException e)
				{
					System.out.println(e.getMessage());
					System.out.println("Cannnot insert data from row count" + count);
				}
			}
			d.closeConnection();
		}
	}
	
	@Test
	public void populateAchivementInject()
	{
		MySqlDAO d = (MySqlDAO) DatabaseFactory.getDAO();
		if (d.isOpen())
		{
			PreparedStatement ps = d.getPreparedStatement(PopulateData.ACHIVEMENT_INJECT[0][0]);
			int length = PopulateData.ACHIVEMENT_INJECT.length;
			for (int count=1 ; count < length; count++)
			{
				try {
				String[] userData = PopulateData.ACHIVEMENT_INJECT[count];
				ps.setString(1, userData[0]);
				ps.setString(2, userData[1]);
				ps.setInt(3, new Integer(userData[2]));
				ps.setString(4, userData[3]);
				ps.setString(5, userData[4]);
				ps.setString(6, userData[5]);
				ps.setString(7, userData[6]);

				ps.executeUpdate();
				}catch (SQLException e)
				{
					System.out.println(e.getMessage());
					System.out.println("Cannnot insert data from row count" + count);
				}
			}
			d.closeConnection();
		}
	}
	
	@Test
	public void populateCar()
	{
		MySqlDAO d = (MySqlDAO) DatabaseFactory.getDAO();
		if (d.isOpen())
		{
			PreparedStatement ps = d.getPreparedStatement(PopulateData.CAR_INJECT[0][0]);
			int length = PopulateData.CAR_INJECT.length;
			for (int count=1 ; count < length; count++)
			{
				try {
				String[] userData = PopulateData.CAR_INJECT[count];
				ps.setString(1, userData[0]);
				ps.setString(2, userData[1]);
				ps.setString(3, userData[2]);
				ps.setInt(4, new Integer(userData[3]));

				ps.executeUpdate();
				}catch (SQLException e)
				{
					System.out.println(e.getMessage());
					System.out.println("Cannnot insert data from row count" + count);
				}
			}
			d.closeConnection();
		}
	}

	@Test
	public void populateCarComments()
	{
		MySqlDAO d = (MySqlDAO) DatabaseFactory.getDAO();
		if (d.isOpen())
		{
			PreparedStatement ps = d.getPreparedStatement(PopulateData.CAR_COMMENTS_INJECT[0][0]);
			int length = PopulateData.CAR_COMMENTS_INJECT.length;
			for (int count=1 ; count < length; count++)
			{
				try {
				String[] userData = PopulateData.CAR_COMMENTS_INJECT[count];
				ps.setString(1, userData[0]);
				ps.setString(2, userData[1]);
				ps.setString(3, userData[2]);

				ps.executeUpdate();
				}catch (SQLException e)
				{
					System.out.println(e.getMessage());
					System.out.println("Cannnot insert data from row count" + count);
				}
			}
			d.closeConnection();
		}
	}

	
	@Test
	public void populateCarPictures()
	{
		MySqlDAO d = (MySqlDAO) DatabaseFactory.getDAO();
		if (d.isOpen())
		{
			PreparedStatement ps = d.getPreparedStatement(PopulateData.CAR_PICTURES_INJECT[0][0]);
			int length = PopulateData.CAR_PICTURES_INJECT.length;
			for (int count=1 ; count < length; count++)
			{
				try {
				String[] userData = PopulateData.CAR_PICTURES_INJECT[count];
				ps.setInt(1, new Integer(userData[0]));
				ps.setInt(2, new Integer(userData[1]));
				ps.setString(3, userData[2]);
				ps.setString(4, userData[3]);

				ps.executeUpdate();
				}catch (SQLException e)
				{
					System.out.println(e.getMessage());
					System.out.println("Cannnot insert data from row count" + count);
				}
			}
			d.closeConnection();
		}
	}
	
	@Test
	public void populateCarModifications()
	{
		MySqlDAO d = (MySqlDAO) DatabaseFactory.getDAO();
		if (d.isOpen())
		{
			PreparedStatement ps = d.getPreparedStatement(PopulateData.CAR_MODIFICATION_INJECT[0][0]);
			int length = PopulateData.CAR_MODIFICATION_INJECT.length;
			for (int count=1 ; count < length; count++)
			{
				try {
				String[] userData = PopulateData.CAR_MODIFICATION_INJECT[count];
				ps.setInt(1, new Integer(userData[0]));
				ps.setString(2, userData[1]);
				ps.setString(3, userData[2]);
				ps.setString(4, userData[3]);

				ps.executeUpdate();
				}catch (SQLException e)
				{
					System.out.println(e.getMessage());
					System.out.println("Cannnot insert data from row count" + count);
				}
			}
			d.closeConnection();
		}
	}
	
	@Test
	public void populateUserTrackDays()
	{
		MySqlDAO d = (MySqlDAO) DatabaseFactory.getDAO();
		if (d.isOpen())
		{
			PreparedStatement ps = d.getPreparedStatement(PopulateData.BOOKEDTRACKDAYS_INJECT[0][0]);
			int length = PopulateData.BOOKEDTRACKDAYS_INJECT.length;
			for (int count=1 ; count < length; count++)
			{
				try {
				String[] userData = PopulateData.BOOKEDTRACKDAYS_INJECT[count];
				ps.setInt(1, new Integer(userData[0]));
				ps.setInt(2, new Integer(userData[1]));
				ps.setBoolean(3, new Boolean(userData[2]));
				ps.setInt(4, new Integer(userData[3]));
				
				ps.executeUpdate();
				}catch (SQLException e)
				{
					System.out.println(e.getMessage());
					System.out.println("Cannnot insert data from row count" + count);
				}
			}
			d.closeConnection();
		}
	}
}
